package com.tong.ft_login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.qihoo360.replugin.loader.a.PluginFragmentActivity;
import com.tong.ft_login.databinding.ActivityLoginBinding;
import com.tong.lib_base.base.plugin.PluginBaseActivity;
import com.tong.lib_base.ft_login.model.UserBean;
import com.tong.lib_base.ft_login.replugin.config.LoginPluginConfig;
import com.tong.lib_base.ft_login.service.impl.LoginImpl;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * author:liuzhitong
 * date:2022/2/18
 * description:
 */
public class LoginActivity extends PluginBaseActivity {

    private ActivityLoginBinding mBinding;

    public static void start(Context context, String param) {
        Intent intent = new Intent(context, LoginActivity.class);
        intent.putExtra("param", param);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_login);
        mBinding.setActivity(this);
        Intent intent = getIntent();
        String param = intent.getStringExtra("param");
        mBinding.textTitle.setText(param);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    /*登录测试*/
    public void checkLogin() {
        Toast.makeText(this, "点击了登录按钮，3秒后自动返回", Toast.LENGTH_SHORT).show();
        new CountDownTimer(3000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {

            }

            @Override
            public void onFinish() {
//                Toast.makeText(LoginActivity.this, "请求成功", Toast.LENGTH_SHORT).show();
//                EventBus.getDefault().post("请求成功");
                sentBroadcastUser(new UserBean());
                finish();
            }
        }.start();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onReceiveMsg(String message) {
    }

    /**
     * 定义广播发送（插件之间传参）
     */
    public void sentBroadcastUser(UserBean user) {
        Log.e("广播","发送方");
        Intent intent = new Intent();
        intent.setAction(LoginPluginConfig.ACTION.LOGIN_SUCCESS_ACTION);
        intent.putExtra(LoginPluginConfig.ACTION.KEY_DATA, "成功");
        sendBroadcast(intent);
        Toast.makeText(LoginActivity.this, "发送方", Toast.LENGTH_SHORT).show();
    }
}
